Skip to content

perf: float64 dot and float32 Add/Sub/Mul/Div/Sqrt on go-simd/floats; delete the private kernels - #129

Merged
tannevaled merged 5 commits into
mainfrom
perf/floats-dot
Nov 22, 2026
Merged

tannevaled merged 5 commits into
mainfrom
perf/floats-dot

Conversation

@tannevaled

@tannevaled tannevaled commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Refs #98
Refs #130

What

One implementation, in go-simd/floats (bumped to v0.35.0), for the float64 dot and the float32 elementwise kernels.

  • dotSIMD is floats.Dot on every architecture. Deleted: dotFMA, dotNEON, dotLASX, dotVSX, dotRVV, dotVX, hasFMA, dot_*.go.
  • Bin32 / Sqrt32 call floats.Add32/Sub32/Mul32/Div32/Sqrt32 (AVX2+SSE2 on amd64, NEON on arm64, and since v0.32.0 LSX, VSX, RVV and VXE kernels ported exactly from this repo's, so loong64, ppc64le, riscv64 and s390x keep their vector kernels through floats). Deleted: the SSE2, NEON, LSX, VSX, RVV and VXE add32/sub32/mul32/div32/sqrt32 kernels and bin32_<arch>.go.
  • Their go-asmgen entries are removed and sum_*.s regenerated for all six architectures.

Contracts

  • Dot: TestDotSIMD asserts |got - exact| <= gamma_n * sum|a_i b_i| (gamma_n = nu/(1-nu), u = 2^-53, the go-blas Dot contract) against a math/big oracle for n = 0..70 and on random data. NaN/Inf propagation unchanged.
  • float32 ops: the contract is bit identity (one IEEE binary32 op per lane; Sqrt32 = float32(math.Sqrt(float64(x)))), and the bit-identity tests against the scalar loop are kept.
  • Length rule: floats processes len(a) and panics on a shorter operand. Bin32 and Sqrt32 cut a, b, src to len(dst), so no ndarray input reaches the floats panic; TestBin32OperandsLongerThanDst pins it. A shorter operand is refused by length, but see kernels: Bin32 and Sqrt32 read past a short operand's length when it has spare capacity #130: a reslice is checked against capacity, not length, so an operand shorter than len(dst) with spare capacity is over-read rather than refused. Not fixed here.

Performance

Single core, AMD EPYC 7773X (linux/amd64), taskset -c 9, GOMAXPROCS=1, NumPy 2.5.5 with OMP/OPENBLAS/MKL_NUM_THREADS=1 (out= preallocated). Kernel-level Go benchmarks (internal/kernels: dotSIMD, MatVecP, Bin32, Sqrt32) built from the merge base 7d5904e ("before") and from the go-simd/floats v0.35.0 head ("after"). Median of 9 interleaved rounds (before, after, NumPy per round). ns/op, lower is better.

Load caveat: cfarm420's 1-minute load was 6.6 to 7.1 when the rounds started and 12.2 at the end (other users; the benchmarks use one pinned core). The 1e6 rows are memory-bound and move by a few percent between rounds; the ratios are interleaved but treat 5 % as noise.

row before after NumPy after/before after/NumPy flag
Dot f64 n=1e3 99.5 87.4 866.6 0.88 0.10
Dot f64 n=1e5 21973 22653 23163 1.03 0.98 within noise (load 9.7 to 11.3)
Dot f64 n=1e6 223076 226375 223836 1.015 1.01 MISS: 1.5 % slower than before, 1 % slower than NumPy; load 9.7 to 11.3, re-measure on a quiet machine
MatVec f64 1024x1024 134520 136570 153224 1.015 0.89
Add f32 n=1e3 108.7 50.1 576.2 0.46 0.09
Add f32 n=1e5 16587 16830 17235 1.01 0.98
Add f32 n=1e6 169042 165417 166363 0.98 0.99
Sub f32 n=1e3 106.9 49.0 543.6 0.46 0.09
Sub f32 n=1e5 16959 17111 17017 1.01 1.01
Sub f32 n=1e6 166932 170098 175831 1.02 0.97
Mul f32 n=1e3 115.7 49.7 561.3 0.43 0.09
Mul f32 n=1e5 16694 16405 17042 0.98 0.96
Mul f32 n=1e6 168793 166909 165814 0.99 1.01
Div f32 n=1e3 273.6 137.6 590.9 0.50 0.23
Div f32 n=1e5 27636 17276 17148 0.63 1.01 1 % slower than NumPy (before: 1.6x slower)
Div f32 n=1e6 281342 178173 166155 0.63 1.07 invalid (load 12.2), to be re-measured; before: 1.7x slower
Sqrt f32 n=1e3 401.5 196.0 861.9 0.49 0.23
Sqrt f32 n=1e5 40210 20231 40506 0.50 0.50
Sqrt f32 n=1e6 397241 204020 398003 0.51 0.51

Against the bar (after no slower than before at every n, and no slower than NumPy where before was not):

  • float64 dot at n=1e6 is 5 % slower than before in the table above (load 12.2 at its end). A second run of the dot rows alone (9 interleaved rounds, before / after, ns/op) gave Dot 1e3 96 / 88 (0.92), Dot 1e5 21973 / 22653 (1.03), Dot 1e6 223076 / 226375 (1.015), MatVec 134520 / 136570 (1.015). That run is also invalid: the load was 9.74 at its start and 11.26 at its end. A third run, at quiet load (cfarm420 1-minute load 2.01 at its start, 3.03 at its end; 9 interleaved rounds, medians, before / after / NumPy, ns/op), settles those rows: Dot 1e5 22731 / 21883 / 23767 (0.963), Dot 1e6 227709 / 228001 / 235189 (1.001, flat within noise), MatVec 1024x1024 142448 / 141506 / 153546 (0.993). After is at or below before on all three, and ahead of NumPy on all three. The bar is met.
  • float32 Div at n=1e6 is 7 % slower than NumPy (and 1 % at 1e5). It is 1.6x faster than before, so this is not a regression. This run (load 6.6→12.2) is invalid; the quiet re-measure with an A/A pair, in deps: go-simd/floats v0.41.0 #131, settles it: Div32 is 4.7-8.9% slower than NumPy past L2 on Zen 3; perf: Div32 4.7-8.9% slower than NumPy past L2 on Zen 3 (memory side, not the divider) go-simd/floats#93. (Correction: an earlier version of this text, quoting go-simd, said Div32 was divider-bound and at parity on Zen 3. go-simd has withdrawn that: NumPy's time at 1e6 is the memory floor that Add32/Mul32 reach, and floats' AVX2 Div32 loses about 15 µs on the memory side.)
  • The v0.32.0 table (dot 1.5 to 3.1x slower than before; Add/Sub/Mul at 1e6 5 to 10 % slower) is superseded: those rows now pass.

Not measured: the other architectures (arm64, loong64, ppc64le, riscv64, s390x). The benchmark file and the NumPy script are not in the repo; they time the same data pattern on both sides. The v0.35.0 bump (go.mod, go.sum, bench/go.mod, bench/go.sum) is commit 4431f47 on top of the v0.32.0 head.

Checks run locally: gofmt -l empty, go test ./... green, buildscan --vet is clean under all 21 CI configurations. The 7 "non-constant format string" vet reports that it found in the internal/kernels/asmgen generators were already on the merge base 7d5904e; they are fixed here in their own commit, 10e6607 (about 80 call sites, now constant formats), and regenerating all six sum_*.s from it is byte-identical (git diff --exit-code -- '*.s').

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 22, 2026 11:49
…ernels

dotSIMD is floats.Dot (v0.28.0) on every architecture. The dotFMA, dotNEON,
dotLASX, dotVSX, dotRVV and dotVX kernels, hasFMA and their generator entries
are gone; sum_*.s regenerated. TestDotSIMD now asserts the stated bound
gamma_n*sum|a_i b_i| against a math/big oracle instead of bit equality.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…the private kernels

Bin32 and Sqrt32 call floats.Add32/Sub32/Mul32/Div32/Sqrt32 on every
architecture (AVX2+SSE2 on amd64, NEON on arm64, portable elsewhere). The
add32/sub32/mul32/div32/sqrt32 SSE2, NEON, LSX, VSX, RVV and VXE kernels and
their generator entries are gone; sum_*.s regenerated. The wrappers still cut
a and b to len(dst), so floats (which processes len(a) and panics on a shorter
operand) never sees unequal lengths; a test pins it. The bit-identity tests
against the scalar loop stay.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tannevaled tannevaled changed the title perf: float64 dot on go-simd/floats.Dot; delete the six private dot kernels perf: float64 dot and float32 Add/Sub/Mul/Div/Sqrt on go-simd/floats; delete the private kernels Nov 22, 2026
tannevaled added a commit to go-simd/floats that referenced this pull request Nov 22, 2026
…SX), riscv64 (RVV), s390x (VXE) (#75)

The float32 elementwise kernels released in v0.30.0 had amd64 AVX2/SSE2 and
arm64 NEON only; go-ndarray/ndarray#129 deletes ndarray's own kernels for these
four architectures, so without them it is a regression there.

Ported exactly from go-ndarray/ndarray at 7d5904e (internal/kernels/bin32_<arch>.go
and asmgen/<arch>/gen.go): one IEEE binary32 operation per lane, Sqrt32 correctly
rounded, generated by go-asmgen (binop32_<arch>_gen.go, diffed in CI like the
amd64 and arm64 ones). Same length contract as binop32.go (first len(a) elements,
dst may alias, an empty slice never reaches a kernel). Gates: hasLASX, hasVSX,
hasRVV, hasVXE (the vars of the other kernels of the package); the portable loop
behind them.

Tests: a gate-off and gate-on rerun of the Add32..Sqrt32 suites per architecture,
and TestSqrt32Exhaustive now on every architecture: all 2^32 patterns on amd64 and
arm64, every 1021st (about 4.2 million) on the emulated ones and the portable
targets. Kernels are unmeasured on hardware (QEMU lanes are not timings).

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
floats v0.32.0 carries the float32 Add/Sub/Mul/Div/Sqrt vector kernels for
loong64, ppc64le, riscv64 and s390x, so no architecture loses its kernel by
the deletion of the private ones.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
tannevaled and others added 2 commits November 22, 2026 16:17
…rnels from 65536 elements)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ntf)

Builder.Raw is printf-style; its first argument was built with fmt.Sprintf or
string concatenation at 80 call sites in the amd64 and arm64 generators, which
go vet reports as a non-constant format string. Pass the text through "%s" or
as a real format with arguments. The regenerated sum_*.s files for all six
architectures (asmgen/regen) are byte-identical: no assembly changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit c769cca into main Nov 22, 2026
13 checks passed
@tannevaled
tannevaled deleted the perf/floats-dot branch November 22, 2026 19:54
tannevaled added a commit to go-blas/blas that referenced this pull request Nov 23, 2026
…ntf) (#11)

Builder.Raw is printf-style; its first argument was built with fmt.Sprintf or
passed as a computed string at 79 call sites in the amd64 and arm64
generators, which go vet reports as a non-constant format string. Pass the
text as a real format with arguments, or through "%s". The regenerated sum_*.s
files for all six architectures (asmgen/regen) are byte-identical: no
assembly changes. Same fix as go-ndarray/ndarray#129.

Refs #10

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant